module uart_string_echo (
    input wire Clk,              // 时钟信号
    input wire Reset_n,          // 复位信号，低有效
    input wire uart_rx,          // UART接收信号
    output wire uart_tx          // UART发送信号
);

    reg [7:0] rx_buffer[0:31];   // 接收字符串的缓冲区
    reg [4:0] rx_index = 0;      // 当前接收的字符索引
    reg [4:0] tx_index = 0;      // 当前发送的字符索引
    reg [7:0] tx_data;           // 当前发送的字符
    reg send_go = 0;             // 启动发送信号
    reg rx_done_flag = 0;        // 接收完成标志
    reg tx_done_flag = 0;        // 发送完成标志

    wire [7:0] rx_data;          // 接收到的字符数据
    wire rx_done;                // 接收完成信号
    wire tx_done;                // 发送完成信号

    // 定义状态常量
    parameter IDLE = 2'b00;
    parameter RECEIVE = 2'b01;
    parameter SEND = 2'b10;
    parameter DONE = 2'b11;

    reg [1:0] current_state, next_state;

    // UART 接收模块实例化
    uart_byte_rx uart_rx_inst (
        .Clk(Clk),
        .Reset_n(Reset_n),
        .uart_rx(uart_rx),
        .Rx_Done(rx_done),
        .Rx_Data(rx_data)
    );

    // UART 发送模块实例化
   uart_tx uart_tx_inst (
       .Clk(Clk),
       .Reset_n(Reset_n),
       .Data(tx_data),
       .send_go(send_go),
       .uart_tx(uart_tx),
       .tx_done(tx_done)
   );

    // 状态机逻辑
   always @(posedge Clk or negedge Reset_n) begin
       if (!Reset_n)
           current_state <= IDLE;
       else
           current_state <= next_state;
   end

   always @(*) begin
       case (current_state)
           IDLE: begin
               if (rx_done)
                   next_state = RECEIVE;
               else
                   next_state = IDLE;
           end
           RECEIVE: begin
               if (rx_index == 31 || rx_done_flag)
                   next_state = SEND;
               else
                   next_state = RECEIVE;
           end
           SEND: begin
               if (tx_done_flag)
                   next_state = DONE;
               else
                   next_state = SEND;
           end
           DONE: begin
               next_state = IDLE;
           end
           default: next_state = IDLE;
       endcase
   end

	// 接收字符逻辑
	always @(posedge Clk or negedge Reset_n) begin
		if (!Reset_n) begin
			rx_index <= 0;
			rx_done_flag <= 0;
		end else if (rx_done) begin
			rx_buffer[rx_index] <= rx_data;
			rx_index <= rx_index + 1'd1;
			if (rx_data == 8'h0D) begin // 检测到回车符号，表示字符串结束
				rx_done_flag <= 1;
			end
		end else if (current_state == DONE) begin
			rx_done_flag <= 0; // 在 DONE 状态复位 rx_done_flag
			rx_index <= 0;
		end
	end
	
	assign LED = send_go;
	
   // 发送字符逻辑
   always @(posedge Clk or negedge Reset_n) begin
   if (!Reset_n) begin
       tx_index <= 0;
       tx_done_flag <= 0;
       send_go <= 0;
       tx_data <= 8'h00; // 初始化 tx_data
   end else if (current_state == SEND) begin
       if (send_go) begin
           if (tx_done) begin
               tx_index <= tx_index + 1'd1;
               if (tx_index == rx_index) begin
                  tx_done_flag <= 1; // 发送完毕
						tx_index <= 0;
						send_go <= 0;
               end else begin
                   tx_data <= rx_buffer[tx_index];
						 rx_buffer[tx_index] <= 8'h00;
                   send_go <= 1; // 继续发送下一个字符
               end
           end
       end else begin
			send_go <= 1; // 启动发送
       end
   end else begin
			send_go <= 0; // 在其他状态中禁用发送
			tx_done_flag <= 0;
		end
	end
endmodule
